package com.springmvc.controller;

import com.springmvc.entity.User;
import com.springmvc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class HomeController {

    @Autowired
    UserService userService;

    @RequestMapping("/home")
    public String home(Model model,Integer current, Integer pageSize,String userName){

        if(current == null || current <= 0){
            current = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }

        List<User> users = userService.page(current,pageSize,userName);

        model.addAttribute("users",users);
        model.addAttribute("current",current);
        if(userName!=null) {
            model.addAttribute("userName", userName);
        }

        Integer total = userService.count();
        Integer pages = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        model.addAttribute("pages",pages);

        return "home";
    }

}
